<template>
  <el-dialog
    :title="!dataForm.doctorId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="doctorName">
      <el-input v-model="dataForm.doctorName" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="doctorSex">
      <el-select v-model="dataForm.doctorSex" placeholder="请选择">
        <el-option
          v-for="item in dataForm.options"
          :key="item.doctorSex"
          :label="item.label"
          :value="item.doctorSex">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="出生年月" prop="birthDate">
      <el-date-picker
        v-model="dataForm.birthDate"
        align="right"
        type="date"
        placeholder="选择日期"
        :picker-options="pickerOptions">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="医生图片" prop="doctorImg">
      <img v-bind:src="dataForm.doctorImg" alt="" style="width: 80px;height: 80px">
      <el-upload
        drag
        :action="url"
        :before-upload="beforeUploadHandle"
        :on-success="successHandle"
        multiple
        :file-list="fileList"
        style="text-align: center;">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只支持jpg、png、gif格式的图片！</div>
      </el-upload>
    </el-form-item>
    <el-form-item label="电话号码" prop="doctorPhone">
      <el-input v-model="dataForm.doctorPhone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="身份证" prop="doctorCertid">
      <el-input v-model="dataForm.doctorCertid" placeholder="身份证"></el-input>
    </el-form-item>
    <el-form-item label="部门" prop="doctorDepartment">
      <el-input v-model="dataForm.doctorDepartment" placeholder="部门"></el-input>
    </el-form-item>
    <el-form-item label="医生职称" prop="doctorTitle">
      <el-input v-model="dataForm.doctorTitle" placeholder="医生职称"></el-input>
    </el-form-item>
    <el-form-item label="家庭住址" prop="doctorAddress">
      <el-input v-model="dataForm.doctorAddress" placeholder="家庭住址"></el-input>
    </el-form-item>
    <el-form-item label="介绍" prop="doctorDesc">
      <el-input v-model="dataForm.doctorDesc" placeholder="介绍"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        url: '',
        num: 0,
        successNum: 0,
        fileList: [],
        pickerOptions: {
          disabledDate (time) {
            return time.getTime() > Date.now()
          },
          shortcuts: [{
            text: '今天',
            onClick (picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }, {
            text: '一周前',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }]
        },
        dataForm: {
          doctorId: 0,
          userId: '',
          doctorName: '',
          doctorSex: '',
          birthDate: '',
          doctorImg: '',
          doctorPhone: '',
          doctorCertid: '',
          doctorDepartment: '',
          doctorTitle: '',
          doctorAddress: '',
          doctorDesc: '',
          options: [{
            doctorSex: '0',
            label: '男'
          }, {
            doctorSex: '1',
            label: '女'
          }]
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          doctorName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          doctorSex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          birthDate: [
            { required: true, message: '出生年月不能为空', trigger: 'blur' }
          ],
          doctorImg: [
            { required: true, message: '医生图片不能为空', trigger: 'blur' }
          ],
          doctorPhone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          doctorCertid: [
            { required: true, message: '身份证不能为空', trigger: 'blur' }
          ],
          doctorDepartment: [
            { required: true, message: '部门不能为空', trigger: 'blur' }
          ],
          doctorTitle: [
            { required: true, message: '医生职称不能为空', trigger: 'blur' }
          ],
          doctorAddress: [
            { required: true, message: '家庭住址不能为空', trigger: 'blur' }
          ],
          doctorDesc: [
            { required: true, message: '介绍不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.url = this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`)
        this.dataForm.doctorId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.doctorId) {
            this.$http({
              url: this.$http.adornUrl(`/hos/doctor/info/${this.dataForm.doctorId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.doctor.userId
                this.dataForm.doctorName = data.doctor.doctorName
                this.dataForm.doctorSex = data.doctor.doctorSex
                this.dataForm.birthDate = data.doctor.birthDate
                this.dataForm.doctorImg = data.doctor.doctorImg
                this.dataForm.doctorPhone = data.doctor.doctorPhone
                this.dataForm.doctorCertid = data.doctor.doctorCertid
                this.dataForm.doctorDepartment = data.doctor.doctorDepartment
                this.dataForm.doctorTitle = data.doctor.doctorTitle
                this.dataForm.doctorAddress = data.doctor.doctorAddress
                this.dataForm.doctorDesc = data.doctor.doctorDesc
              }
            })
          }
        })
      },
      // 上传之前
      beforeUploadHandle (file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        this.num++
      },
      // 上传成功
      successHandle (response, file, fileList) {
        this.fileList = fileList
        this.successNum++
        if (response && response.code === 0) {
          this.dataForm.doctorImg = response.url
          if (this.num === this.successNum) {
            this.$confirm('操作成功, 是否继续操作?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(() => {
              this.visible = false
            })
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hos/doctor/${!this.dataForm.doctorId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'doctorId': this.dataForm.doctorId || undefined,
                'userId': this.dataForm.userId,
                'doctorName': this.dataForm.doctorName,
                'doctorSex': this.dataForm.doctorSex,
                'birthDate': this.dataForm.birthDate,
                'doctorImg': this.dataForm.doctorImg,
                'doctorPhone': this.dataForm.doctorPhone,
                'doctorCertid': this.dataForm.doctorCertid,
                'doctorDepartment': this.dataForm.doctorDepartment,
                'doctorTitle': this.dataForm.doctorTitle,
                'doctorAddress': this.dataForm.doctorAddress,
                'doctorDesc': this.dataForm.doctorDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
